Skip to content

clippy: make tests work in stage 1 #144027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

RalfJung
Copy link
Member

This finally fixes #78717 :)

Similar to what Miri already does, the clippy test step needs to carefully consider which compiler is used to build clippy and which compiler is linked into clippy (and thus must be used to build the test dependencies). On top of that we have some extra complications that Miri avoided by using cargo-miri for building its test dependencies: we need cargo to use the right rustc and the right sysroot, but rust-lang/cargo#4423 makes this quite hard to do. See the long comment in src/tools/clippy/tests/compile-test.rs for details.

Some clippy tests tried to import rustc crates; that fundamentally requires a full bootstrap loop so it cannot work in stage 1. I had to kind of guess what those tests were doing so I don't know if my changes there make any sense.

Cc @flip1995 @Kobzol

@rustbot
Copy link
Collaborator

rustbot commented Jul 16, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-clippy Relevant to the Clippy team. labels Jul 16, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 16, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

// set `--sysroot`, so we need to use bootstrap's rustc wrapper. That wrapper
// however has some staging logic that is hurting us here, so to work around
// that we set both the "real" and "staging" rustc to TEST_RUSTC, including the
// associated library paths.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to use a different approach here... somehow, aux builds are working for clippy tests. It seems to be using clippy itself as the compiler for that, and that's somehow okay? So, we could try to do the same here, instead of all this TEST_RUSTC stuff. (That would also be closer to what Miri does.)

But TBH I found something that works so I am not overly inclined to debug a different approach. ;)

@Kobzol
Copy link
Member

Kobzol commented Jul 17, 2025

FWIW, I have a WIP branch (there are so many stacked changes to bootstrap that I'm doing rn that it takes a while to unload) where I refactor the building of tools, so that the correct compiler should always be chosen, and we don't need to do this careful dance for each tool and bootstrap Step separately. Essentially we'd invert the compiler "flow" in the tool, so that we pass it the build compiler instead of figuring it out from the tool, and also hopefully we'll be able to get rid of the implicit rmeta/rlib copy into the build compiler's sysroot and make that explicit.

Anyway, I'm fine with this specific fix if it helps the stage 1 Clippy tests in the meantime (it seems like there are some other blockers in the tests themselves, which can be resolved in parallel), just wanted to note that hopefully soonish this will all be unified away so that we always do The Right Thing™.

@Mark-Simulacrum
Copy link
Member

@bors r+ rollup=iffy

@bors
Copy link
Collaborator

bors commented Jul 19, 2025

📌 Commit a70171b has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 19, 2025
bors added a commit that referenced this pull request Jul 20, 2025
clippy: make tests work in stage 1

This finally fixes #78717 :)

Similar to what Miri already does, the clippy test step needs to carefully consider  which compiler is used to build clippy and which compiler is linked into clippy (and thus must be used to build the test dependencies). On top of that we have some extra complications that Miri avoided by using `cargo-miri` for building its test dependencies: we need cargo to use the right rustc and the right sysroot, but rust-lang/cargo#4423 makes this quite hard to do. See the long comment in `src/tools/clippy/tests/compile-test.rs` for details.

Some clippy tests tried to import rustc crates; that fundamentally requires a full bootstrap loop so it cannot work in stage 1. I had to kind of guess what those tests were doing so I don't know if my changes there make any sense.

Cc `@flip1995` `@Kobzol`
@bors
Copy link
Collaborator

bors commented Jul 20, 2025

⌛ Testing commit a70171b with merge 6fc5806...

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-msvc-ext3 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
tests/ui/crashes/third-party/conf_allowlisted.rs ... ok
tests/ui/size_of_in_element_count/functions.rs ... ok

FAILED TEST: tests/ui/cast_alignment.rs
command: "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\x86_64-pc-windows-msvc\\release\\clippy-driver.exe" "--error-format=json" "--emit=metadata" "-Aunused" "-Ainternal_features" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Dwarnings" "-Ldependency=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\release\\deps" "--sysroot=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage2" "--out-dir" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\tests\\ui" "tests\\ui\\cast_alignment.rs" "--extern" "futures=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libfutures-00219f7994766d4f.rlib" "--extern" "futures=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libfutures-00219f7994766d4f.rmeta" "--extern" "if_chain=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libif_chain-22dbd8171cd76089.rlib" "--extern" "if_chain=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libif_chain-22dbd8171cd76089.rmeta" "--extern" "itertools=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libitertools-9449b9ceafe70c0f.rlib" "--extern" "itertools=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libitertools-9449b9ceafe70c0f.rmeta" "--extern" "parking_lot=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libparking_lot-f7590b993db3a55b.rlib" "--extern" "parking_lot=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libparking_lot-f7590b993db3a55b.rmeta" "--extern" "quote=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libquote-39fdd38cdb14299d.rlib" "--extern" "quote=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libquote-39fdd38cdb14299d.rmeta" "--extern" "regex=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libregex-b51f98d36b5c1725.rlib" "--extern" "regex=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libregex-b51f98d36b5c1725.rmeta" "--extern" "serde=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libserde-921cbfe73fc218e0.rlib" "--extern" "serde=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libserde-921cbfe73fc218e0.rmeta" "--extern" "syn=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libsyn-495b6e3d873bec44.rlib" "--extern" "syn=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libsyn-495b6e3d873bec44.rmeta" "--extern" "tokio=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libtokio-427cba4286f32e86.rlib" "--extern" "tokio=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libtokio-427cba4286f32e86.rmeta" "-L" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps" "-L" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\debug\\deps" "--edition" "2024"

error: actual output differed from expected
Execute `./x test src/tools/clippy --bless` to update `tests/ui/cast_alignment.stderr` to the actual output
--- tests/ui/cast_alignment.stderr
+++ <stderr output>
-error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
+error[E0463]: can't find crate for `libc`
-  --> tests/ui/cast_alignment.rs:19:5
+  --> tests/ui/cast_alignment.rs:13:1
    |
-LL |     (&1u8 as *const u8) as *const u16;
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |
-   = note: `-D clippy::cast-ptr-alignment` implied by `-D warnings`
-   = help: to override `-D warnings` add `#[allow(clippy::cast_ptr_alignment)]`
+LL | extern crate libc;
+   | ^^^^^^^^^^^^^^^^^^ can't find crate
 
-error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
-  --> tests/ui/cast_alignment.rs:22:5
-   |
-LL |     (&mut 1u8 as *mut u8) as *mut u16;
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+error: aborting due to 1 previous error
 
-error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
-  --> tests/ui/cast_alignment.rs:26:5
-   |
-LL |     (&1u8 as *const u8).cast::<u16>();
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
-error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
-  --> tests/ui/cast_alignment.rs:29:5
-   |
-LL |     (&mut 1u8 as *mut u8).cast::<u16>();
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-
-error: aborting due to 4 previous errors
-
+For more information about this error, try `rustc --explain E0463`.
---



FAILED TEST: tests/ui/strlen_on_c_strings.rs
command: "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\x86_64-pc-windows-msvc\\release\\clippy-driver.exe" "--error-format=json" "--emit=metadata" "-Aunused" "-Ainternal_features" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Dwarnings" "-Ldependency=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\release\\deps" "--sysroot=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage2" "--out-dir" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\tests\\ui" "tests\\ui\\strlen_on_c_strings.rs" "--extern" "futures=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libfutures-00219f7994766d4f.rlib" "--extern" "futures=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libfutures-00219f7994766d4f.rmeta" "--extern" "if_chain=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libif_chain-22dbd8171cd76089.rlib" "--extern" "if_chain=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libif_chain-22dbd8171cd76089.rmeta" "--extern" "itertools=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libitertools-9449b9ceafe70c0f.rlib" "--extern" "itertools=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libitertools-9449b9ceafe70c0f.rmeta" "--extern" "parking_lot=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libparking_lot-f7590b993db3a55b.rlib" "--extern" "parking_lot=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libparking_lot-f7590b993db3a55b.rmeta" "--extern" "quote=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libquote-39fdd38cdb14299d.rlib" "--extern" "quote=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libquote-39fdd38cdb14299d.rmeta" "--extern" "regex=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libregex-b51f98d36b5c1725.rlib" "--extern" "regex=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libregex-b51f98d36b5c1725.rmeta" "--extern" "serde=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libserde-921cbfe73fc218e0.rlib" "--extern" "serde=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libserde-921cbfe73fc218e0.rmeta" "--extern" "syn=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libsyn-495b6e3d873bec44.rlib" "--extern" "syn=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libsyn-495b6e3d873bec44.rmeta" "--extern" "tokio=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libtokio-427cba4286f32e86.rlib" "--extern" "tokio=D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps\\libtokio-427cba4286f32e86.rmeta" "-L" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\x86_64-pc-windows-msvc\\debug\\deps" "-L" "D:\\a\\rust\\rust\\build\\x86_64-pc-windows-msvc\\stage1-tools\\ui_test\\0\\debug\\deps" "--edition" "2024"

error: actual output differed from expected
Execute `./x test src/tools/clippy --bless` to update `tests/ui/strlen_on_c_strings.stderr` to the actual output
--- tests/ui/strlen_on_c_strings.stderr
+++ <stderr output>
-error: using `libc::strlen` on a `CString` or `CStr` value
+error[E0463]: can't find crate for `libc`
-  --> tests/ui/strlen_on_c_strings.rs:13:13
+  --> tests/ui/strlen_on_c_strings.rs:4:1
    |
-LL |     let _ = unsafe { libc::strlen(cstring.as_ptr()) };
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cstring.as_bytes().len()`
-   |
-   = note: `-D clippy::strlen-on-c-strings` implied by `-D warnings`
-   = help: to override `-D warnings` add `#[allow(clippy::strlen_on_c_strings)]`
+LL | extern crate libc;
+   | ^^^^^^^^^^^^^^^^^^ can't find crate
 
-error: using `libc::strlen` on a `CString` or `CStr` value
-  --> tests/ui/strlen_on_c_strings.rs:18:13
-   |
-LL |     let _ = unsafe { libc::strlen(cstr.as_ptr()) };
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cstr.to_bytes().len()`
+error: aborting due to 1 previous error
 
-error: using `libc::strlen` on a `CString` or `CStr` value
-  --> tests/ui/strlen_on_c_strings.rs:21:13
-   |
-LL |     let _ = unsafe { strlen(cstr.as_ptr()) };
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cstr.to_bytes().len()`
-
-error: using `libc::strlen` on a `CString` or `CStr` value
-  --> tests/ui/strlen_on_c_strings.rs:25:22
-   |
-LL |     let _ = unsafe { strlen((*pcstr).as_ptr()) };
-   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(*pcstr).to_bytes().len()`
-
-error: using `libc::strlen` on a `CString` or `CStr` value
-  --> tests/ui/strlen_on_c_strings.rs:31:22
-   |
-LL |     let _ = unsafe { strlen(unsafe_identity(cstr).as_ptr()) };
-   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unsafe_identity(cstr).to_bytes().len()`
-
-error: using `libc::strlen` on a `CString` or `CStr` value
-  --> tests/ui/strlen_on_c_strings.rs:33:13
-   |
-LL |     let _ = unsafe { strlen(unsafe { unsafe_identity(cstr) }.as_ptr()) };
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unsafe { unsafe_identity(cstr) }.to_bytes().len()`
-
-error: using `libc::strlen` on a `CString` or `CStr` value
-  --> tests/ui/strlen_on_c_strings.rs:37:22
-   |
-LL |     let _ = unsafe { strlen(f(cstr).as_ptr()) };
-   |                      ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `f(cstr).to_bytes().len()`
-
-error: aborting due to 7 previous errors
-
+For more information about this error, try `rustc --explain E0463`.

@bors
Copy link
Collaborator

bors commented Jul 20, 2025

💔 Test failed - checks-actions

@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jul 20, 2025
@bors bors added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-clippy Relevant to the Clippy team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x.py test --stage 0 src/tools/clippy does not work
8 participants